package com.example.exam.sysResource.controller;

import com.example.exam.authorization.service.AuthorizationService;
import com.example.exam.common.ResultBean;
import com.example.exam.common.ResultBeanCodeEnum;
import com.example.exam.sysDictInfo.model.SysDictInfoBean;
import com.example.exam.sysPermission.entity.SysPermissionBean;
import com.example.exam.sysPermission.service.SysPermissionService;
import com.example.exam.sysResource.model.SysResourceBean;
import com.example.exam.sysResource.service.SysResourceService;
import com.example.exam.user.model.UserRoleBean;
import com.example.exam.user.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sysResource")
public class SysResourceController {
    private static String base_path="/sysResource/";
    @Autowired
    SysResourceService sysResourceService;
    @Autowired
    SysPermissionService sysPermissionService;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    AuthorizationService authorizationService;

    /**
     * 去添加页面
     * @return
     */
    @GetMapping("/toAddResource")
        /** 
            * @Description: 去菜单添加页面
            * @Param: [] 
            * @return: java.lang.String 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public String toAddResource(){
        return base_path+"addResource";
    }

    /**
     * 去编辑页面
     * @param sysResourceBean
     * @param request
     * @return
     */
    @GetMapping("/toEditResource")
        /** 
            * @Description:  
            * @Param: [sysResourceBean, request] 
            * @return: java.lang.String 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public String toEditResource(SysResourceBean sysResourceBean, HttpServletRequest request){
        SysResourceBean srb=sysResourceService.selectOne(sysResourceBean.getId());
        request.setAttribute("srb",srb);
        return base_path+"editResource";
    }
    @RequestMapping("/toSysResource")
        /** 
            * @Description:  去菜单信息页面
            * @Param: [request] 
            * @return: java.lang.String 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public String toSysResource(HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer userId=(Integer) session.getAttribute("id");
        ResultBean resultBean  = new ResultBean();
        List<UserRoleBean> list=userRoleService.authorization(userId);
        if(list!=null){
            for (int i=0;i<list.size();i++){
                if("SysResource|toEdit".equals(list.get(i).getPre())){
                    request.setAttribute("update",list.get(i));
                }else if("SysResource|toDelete".equals(list.get(i).getPre())){
                    request.setAttribute("delete",list.get(i));
                }else if("SysResource|toGrant".equals(list.get(i).getPre())){
                    request.setAttribute("grant",list.get(i));
                }else if ("SysResource|toAdd".equals(list.get(i).getPre())){
                    request.setAttribute("add",list.get(i));
                }
            }
        }
        Integer id=(Integer)session.getAttribute("id");
        String post=authorizationService.findPost(id);
        if (post!=null){
            if("八岗".equals(post)||"七岗".equals(post)){
                post="八岗";
            }else{
                post=null;
            }
        }
        Integer num=authorizationService.findRole(id);
        request.setAttribute("post",post);
        request.setAttribute("num",num);
        return base_path+"sysResource";
    }
    @GetMapping("/list")
    @ResponseBody
        /** 
            * @Description:  查询菜单列表
            * @Param: [request, pageNo, id] 
            * @return: java.lang.Object 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public Object list(HttpServletRequest request, Integer pageNo,Integer id) {
        ResultBean resultBean;
        resultBean = sysResourceService.getResourceList(id);
        return resultBean;
    }
    @PostMapping("/insertResource")
    @ResponseBody
        /** 
            * @Description: 添加菜单信息
            * @Param: [sysResourceBean, request] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean insertResource(SysResourceBean sysResourceBean,HttpServletRequest request){
        ResultBean resultBean=null;
        try{
            HttpSession session=request.getSession();
            Integer userId=(Integer) session.getAttribute("id");
            sysResourceBean.setCreateUser(userId);
            resultBean=sysResourceService.insertResource(sysResourceBean);
            resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
        }
        return resultBean;
    }
    @PostMapping("/deleteResource")
    @ResponseBody
        /** 
            * @Description:  删除一条菜单信息
            * @Param: [sysResourceBean] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean deleteResource(SysResourceBean sysResourceBean){
        ResultBean resultBean=new ResultBean();
        Integer i=sysResourceService.deleteResource(sysResourceBean.getId());
        if (i==1){
            resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
        }else{
            resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
        }
        return resultBean;
    }
    @PostMapping("/updateResource")
    @ResponseBody
        /** 
            * @Description:  修改菜单信息
            * @Param: [sysResourceBean, request] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean updateResource(SysResourceBean sysResourceBean,HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer userId=(Integer) session.getAttribute("id");
        sysResourceBean.setCreateUser(userId);
        ResultBean resultBean=new ResultBean();
        Integer i=sysResourceService.updateResource(sysResourceBean);
        if (i==1){
            resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
        }else{
            resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
        }
        return resultBean;
    }
    @RequestMapping("/toResourceList")
    @ResponseBody
        /** 
            * @Description:  去菜单信息列表
            * @Param: [request, pid] 
            * @return: java.lang.Object 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public Object queryResource(HttpServletRequest request, Integer pid) {
        List<SysResourceBean> childList = sysResourceService.queryResource(pid);
        return childList;
    }
    @RequestMapping("/toParent")
    @ResponseBody
        /** 
            * @Description: 查询父级菜单
            * @Param: [request] 
            * @return: java.lang.Object 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public Object queryParentResource(HttpServletRequest request) {
        List<SysResourceBean> list = sysResourceService.queryParentResource();
        return list;
    }
    @GetMapping("/toGrant")
        /** 
            * @Description:  去权限配置页面
            * @Param: [request, id] 
            * @return: java.lang.Object 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public Object toGrant(HttpServletRequest request, Integer id) {
        List<SysDictInfoBean> list = sysResourceService.queryChild(1);
        request.setAttribute("list", list);
        SysResourceBean sysResourceBean = sysResourceService.findById(id);
        request.setAttribute("sysResourceBean", sysResourceBean);
        List<SysPermissionBean> listPermission = sysPermissionService.queryPermissionList(id);
        for (int i=0;i<list.size();i++){
            for (int j=0;j<listPermission.size();j++){
                if(list.get(i).getName().equals(listPermission.get(j).getName())){
                    list.get(i).setDescribe(null);
                }
            }
        }
        return base_path + "grant";
    }
    @PostMapping("/savePermission")
    @ResponseBody
        /** 
            * @Description:  添加/删除权限
            * @Param: [request, id, permission] 
            * @return: java.lang.Object 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public Object savePersmission(HttpServletRequest request, Integer id, String permission) {
        ResultBean resultBean = new ResultBean();
        try {
            resultBean = sysPermissionService.savePermissionByResourceId(id, permission,request);
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
        }
        return resultBean;
    }
}
